package com.yiwyn.Creational.prototype;


import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Warmup(iterations = 1, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1)
@Fork(1)
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
public class PrototypeDemo {


    @Benchmark
    public List<DroolsEntity> cloneCreate() {
        List<DroolsEntity> droolsEntities = new ArrayList<>();
        DroolsEntity droolsEntity = new DroolsEntity("1", "1");

        for (int i = 0; i < 10; i++) {
            DroolsEntity clone = droolsEntity.clone();
            clone.setDroolsRule(String.valueOf(i));
            clone.setDroolsName(String.valueOf(i));
            droolsEntities.add(clone);
        }
        return droolsEntities;
    }

    @Benchmark
    public List<DroolsEntity> newCreate() {
        List<DroolsEntity> droolsEntities = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            droolsEntities.add(new DroolsEntity(String.valueOf(i), String.valueOf(i)));
        }
        return droolsEntities;
    }


    public static void main(String[] args) throws RunnerException {
        Options options = new OptionsBuilder()
                .include(PrototypeDemo.class.getSimpleName())
                // .addProfiler(GCProfiler.class)
                .build();

        new Runner(options).run();

    }
}
